package com.itfenghuang.day14;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.LinkedList;

//多人聊天服务端
public class A05_ServerChat {
    //定义一个集合 存储socket对象
    private static ArrayList<Socket> sockets=new ArrayList<>();
    //用来存储 接收到的消息
    private static LinkedList<String> message=new LinkedList<>();

    public static void main(String[] args) throws IOException {
        /**
         *   1. 接收客户端的消息,并将消息回送给其他客户端
         */
        ServerSocket ss = new ServerSocket(10086);

        //监听连接的线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        //因为是多人聊天,每连接一个人都会生成一个socket
                        Socket socket = ss.accept();
                        System.out.println("有人上线了");
                        //将socket对象存到集合中
                        sockets.add(socket);


                        //线程 用来接收客户端发送的消息
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                //接收客户端消息,循环接收
                                    try {
                                        InputStream is = socket.getInputStream();
                                        //创建字符缓冲输入流
                                        BufferedReader br = new BufferedReader(new InputStreamReader(is));
                                        while (true){
                                            String s = br.readLine();
                                            System.out.println(s);
                                            message.addFirst(s);
                                        }
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }

                            }
                        }).start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        //将消息回送给客户端的线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (!message.isEmpty()){
                        //获取集合中的元素
                        String last = message.getLast();
                        message.removeLast();
                        for (Socket socket : sockets) {
                            try {
                                OutputStream os = socket.getOutputStream();
                                //创建字符缓冲输入流
                                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
                                bw.write(last);
                                bw.newLine();
                                bw.flush();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }).start();

    }
}
